<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\goods\GoodsFindModel;
use think\Request;

/**
 * GoodsFind 商品发现
 * Class GoodsFind
 * @package app\controller
 */
class GoodsFind extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new GoodsFindModel();
    }

    /**
     * 分页列表
     * @param string $status
     * @param string $keyword
     * @param string $order_out_status
     * @param string $order_out_handle_status
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     */
    public function getPage($status = '', $keyword = '', $page = 1, $limit = 10)
    {
        $field = "a.*,b.nick_name,c.goods_title,c.goods_img";
        $data = $this->model->getPageData($status, $keyword, $page, $limit, $field);
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }


    /**
     * 单条信息
     * @param $id
     * @return \think\response\Json
     */
    public function getInfo($id)
    {
        $field = "a.*,b.nick_name,c.goods_title,c.goods_img";
        $data = $this->model->getInfo($id, $field);
        $data['photo'] = explode(',', $data['photo']);
        if (!empty($data)) {
            return self::returnMsg(1, $data);
        } else {
            return self::returnMsg(1001);
        }
    }

    /**
     * 保存
     * @param Request $request
     * @return \think\response\Json
     */
    public function onSave(Request $request)
    {
        $formData = $request->param();
        unset($formData['delete_time']);
        unset($formData['create_time']);
        unset($formData['update_time']);
        if (empty($formData['id'])) {
            $validate = 'app\validate\GoodsFindValidate.add';
        } else {
            $validate = 'app\validate\GoodsFindValidate';
        }
        if (isset($formData['create_time']) || isset($formData['update_time'])) {
            $formData['create_time'] = strtotime($formData['create_time']) ? strtotime($formData['create_time']) : $formData['create_time'];
            $formData['update_time'] = strtotime($formData['update_time']) ? strtotime($formData['update_time']) : $formData['update_time'];
        }
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, false, false);//修改是否添加进缓存。。。
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 上线状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function onStatus(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'status' => $request->param('status'),
        ];

        $validate = 'app\validate\GoodsFindValidate.upstatus';
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, false, false);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 订单退款设置
     * @param Request $request
     * @return \think\response\Json
     */
    public function onOrderOutEdit(Request $request)
    {
        $formData = [
            "id" => $request->param("id"),
            "order_out_handle_status" => $request->param("order_out_handle_status"),
            "order_out_not_text" => $request->param("order_out_not_text"),
        ];

        $validate = 'app\validate\OrderValidate.CheckOrderOut';
        $result = $this->validate($formData, $validate);

        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, false, false);//修改是否添加进缓存。。。
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function onDelete($id)
    {
        $flag = $this->model::destroy(['id' => $id]);
        if (false !== $flag) {
            $this->model->delRedis($id);
            $this->model->redis->zRem($this->model->redis_key . ':rank', $id);
            $this->model->addToRecycle($id, 'GoodsFindModel', '商品管理-发现', $this->currentUser['user_id'], $this->currentUser['user_type'],'goods');
            return self::returnMsg(1);
        } else {
            return self::returnMsg(1002);
        }
    }


}
